<script setup lang="ts">
import { onMounted } from "vue";
import { storeToRefs } from "pinia";
import { ElMessage } from "element-plus";
import useTabNavStore from "@/pinia/modules/product/attributeStore/tabNavStore/tabNavStore.ts";

defineProps({
  screen: {
    type: Number,
    default: 0,
  },
});
const tabNavStore = useTabNavStore();
const { categoryId, category1List, category2List, category3List } = storeToRefs(tabNavStore);

/* 获取一级分类列表的回调函数 */
const fetchCategory1List = async () => {
  try {
    await tabNavStore.fetchCategory1List();
    ElMessage.success("一级分类获取成功");
  } catch (error) {
    ElMessage.error(error);
  }
};

/* 获取二级分类列表的回调函数 */
const fetchCategory2List = async () => {
  try {
    await tabNavStore.fetchCategory2List();
    ElMessage.success("二级分类获取成功");
  } catch (error) {
    ElMessage.error(error);
  }
};

/* 获取三级分类列表的回调函数 */
const fetchCategory3List = async () => {
  try {
    await tabNavStore.fetchCategory3List();
    ElMessage.success("三级分类获取成功");
  } catch (error) {
    ElMessage.error(error);
  }
};

onMounted(() => {
  fetchCategory1List();
});
</script>

<template>
  <el-card>
    <el-form inline :model="categoryId">
      <el-form-item label="一级分类">
        <el-select v-model="categoryId.category1Id" placeholder="请选择一级分类" @change="fetchCategory2List" :disabled="screen===1">
          <el-option v-for="(category1) in category1List" :value="category1.id" :label="category1.name" :key="category1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="categoryId.category2Id" placeholder="请选择二级分类" @change="fetchCategory3List" :disabled="screen===1">
          <el-option v-for="(category2) in category2List" :value="category2.id" :label="category2.name" :key="category2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="categoryId.category3Id" placeholder="请选择三级分类" :disabled="screen===1">
          <el-option v-for="(category3) in category3List" :value="category3.id" :label="category3.name" :key="category3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>
